package com.lty.aio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;

/***
 * 
* @ClassName: AIOClient 
* @Description: 异步io客户端
* @author Lau 
* @date 2017年7月9日 上午10:20:44 
*
 */
public class AIOClient {
	private AsynchronousSocketChannel client;
	
	public AIOClient(String host, int port) throws Exception{
		client = AsynchronousSocketChannel.open();
		Future<?> future = client.connect(new InetSocketAddress(host, port));
		System.out.println(future.get());
	}
	
	//向服务端发送数据
	public void write(byte b){
		ByteBuffer byteBuffer = ByteBuffer.allocate(32);
		byteBuffer.put(b);
		byteBuffer.flip();
		client.write(byteBuffer);
	}
	
	public static void main(String[] args) throws Exception {
		String host = "127.0.0.1";
		int port = 2017;
		AIOClient client = new AIOClient(host, port);
		client.write((byte)50);
	}
}
